package com.block.sms.service.send;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.block.common.service.LogFactory;
import com.block.common.util.LocalDateUtil;
import com.block.db.entity.SmsConfigInfo;
import com.block.db.entity.SmsTemplateInfo;
import com.block.sms.dto.*;
import com.block.sms.service.ISmsSendService;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class MeiXinSmsSendServiceImpl implements ISmsSendService {

    private static final Logger log = LogFactory.getCommLog();

    @Override
    public SmsResultDTO sendSms(SmsParamDTO smsParamDTO, SmsTemplateInfo smsTemplate, SmsConfigInfo smsConfigInfo) {

        SmsResultDTO resultDTO = new SmsResultDTO();
        JSONArray paramArr = smsParamDTO.getParamArray();

        String smsCode = paramArr.getString(0);

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("mobile", smsParamDTO.getMobie());
        paramMap.put("code", smsCode);

        String bodyResult = HttpUtil.createPost(smsConfigInfo.getSendUrl())
                .body(JSON.toJSONString(paramMap))
                .execute()
                .body();
        log.info("【美信短信服务】发送结束【requesturl】{}【paramMap】{}【bodyResult】{}",
                smsConfigInfo.getSendUrl(), JSON.toJSONString(paramMap), bodyResult);

        JSONObject resultJson = JSON.parseObject(bodyResult);
        String code = resultJson.getString("code");

        resultDTO.setResult(bodyResult);
        resultDTO.setCode(String.valueOf(code));
//        resultDTO.setMessage(message);
//        resultDTO.setOutOrderNo(String.valueOf(msg_id));
        if (StrUtil.equals("200", code)) {
            resultDTO.setSuccess(true);
            return resultDTO;
        }
        resultDTO.setMessage(bodyResult);
        return resultDTO;
    }

    @Override
    public SmsBalanceDTO queryBalanceInfo(SmsConfigInfo smsConfigInfo) {

        return null;
    }

    @Override
    public QueryResultDTO querySendResult(QueryParamDTO queryParamDTO, SmsConfigInfo smsConfigInfo) {
        return null;
    }

    public static void main(String[] args) {

        String mobile = "13298282779";
        String smsCode = "678909";
        String reqUrl = "https://www.csdk.fun/api/login/sendCodeMeiXin";

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("mobile", mobile);
        paramMap.put("code", smsCode);

        String bodyResult = HttpUtil.createPost(reqUrl)
                .body(JSON.toJSONString(paramMap))
                .execute()
                .body();

        System.out.println(bodyResult);
    }
}
